Imports System.Configuration
Imports System.Data
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.Configuration
Imports System.Data.SqlClient




Public Partial Class TheatreStaff_Movies
	Inherits System.Web.UI.Page
	Protected Sub Page_Load(sender As Object, e As EventArgs)
		Dim vali1 As CompareValidator = DirectCast(dtlMovie.FindControl("CompareValidator1"), CompareValidator)


		srcMovie.SelectParameters("username").DefaultValue = Page.User.Identity.Name
		srcMoviegrd.SelectParameters("username").DefaultValue = Page.User.Identity.Name

		Dim connectionString As String
		connectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

		Dim con As New SqlConnection(connectionString)
		Dim cmd As New SqlCommand("SELECT TheatreId from TheatreStaff WHERE TheatreStaffUsername=@username", con)

		cmd.Parameters.AddWithValue("@username", Page.User.Identity.Name)

		Using con
			con.Open()
			Dim TheatreID As Object = cmd.ExecuteScalar()
			If TheatreID IsNot Nothing Then
				lblId.Text = TheatreID.ToString()
			End If
		End Using
		Dim con1 As New SqlConnection(connectionString)
		Dim cmd1 As New SqlCommand("SELECT Theatrename from Theatre WHERE Theatreid=@theatreid", con1)

		cmd1.Parameters.AddWithValue("@theatreid", lblId.Text)


		Using con1
			con1.Open()
			Dim Theatrename As Object = cmd1.ExecuteScalar()

			If Theatrename IsNot Nothing Then

				Label2.Text = Theatrename.ToString()
			End If
		End Using

	End Sub
    Protected Function test(ByVal sold As Object) As [Boolean]

        Dim test1 As Boolean = True

        Dim val As Double = Convert.ToDouble(sold)
        If val > 0 Then

            Return False
        Else
            Return True
        End If


    End Function
    Protected Sub btnOk_Click(ByVal sender As Object, ByVal e As EventArgs)


        srcMovie.InsertParameters("TheatreId").DefaultValue = lblId.Text

        Label15.Text = String.Empty
        Label16.Text = String.Empty
    End Sub

    Protected Sub dtlSchedule_ItemUpdated(ByVal sender As Object, ByVal e As DetailsViewUpdatedEventArgs)

        grdMovie.DataBind()
        Label15.Text = String.Empty
        Label16.Text = String.Empty
    End Sub
    Protected Sub function1(ByVal source As Object, ByVal args As ServerValidateEventArgs)

        Dim tb2 As TextBox = DirectCast(dtlMovie.FindControl("txtMovieTitle"), TextBox)
        Dim tb3 As TextBox = DirectCast(dtlMovie.FindControl("txtMovieDirector"), TextBox)
        If Label3.Text.Equals(tb2.Text) And Label6.Text.Equals(tb3.Text) Then
            Label3.Text = tb2.Text

            args.IsValid = True
        Else

            Dim connectionString As String
            connectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
            Dim con As New SqlConnection(connectionString)
            Dim cmd1 As New SqlCommand("SELECT MovieTitle,MovieDirector from Movie WHERE TheatreId=@TheatreId AND MovieTitle=@MovieTitle AND MovieDirector=@MovieDirector", con)
            Dim ParamString As String = lblId.Text
            Dim Showing As [Boolean]
            cmd1.Parameters.AddWithValue("@TheatreId", ParamString)
            cmd1.Parameters.AddWithValue("@MovieTitle", tb2.Text)
            cmd1.Parameters.AddWithValue("@MovieDirector", tb3.Text)
            Using con
                con.Open()
                Dim reader As SqlDataReader = cmd1.ExecuteReader()
                If reader.HasRows Then
                    args.IsValid = False
                    Showing = False
                    Label4.Text = "match"
                Else
                    Label4.Text = "NO maetch"
                    Showing = True
                    args.IsValid = True
                End If
            End Using
        End If
    End Sub
    Protected Sub function2(ByVal sender As Object, ByVal e As EventArgs)
        Dim tb2 As TextBox = DirectCast(dtlMovie.FindControl("txtMovieTitle"), TextBox)

        Label3.Text = tb2.Text


    End Sub
    Protected Sub function3(ByVal sender As Object, ByVal e As EventArgs)

        Dim tb3 As TextBox = DirectCast(dtlMovie.FindControl("txtMovieDirector"), TextBox)

        Label6.Text = tb3.Text

    End Sub
    'protected void InsertMovie_Click(object sender, EventArgs e)
    '{
    '    Label15.Text = string.Empty;
    '    Label16.Text = string.Empty;

    '    string connectionString;
    '    connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    '    SqlConnection con = new SqlConnection(connectionString);
    '    SqlConnection con1 = new SqlConnection(connectionString);
    '    SqlCommand cmd1 = new SqlCommand("INSERT Movie(CategoryId,MovieTitle,MovieDirector,Showing,MovieDescription,TheatreId,MovieDuration,imagefname,Rating,Trailers,TheatreDateReleased,maincast) values (@CategoryId,@MovieTitle,@MovieDirector,@Showing,@MovieDescription,@TheatreId,@MovieDuration,@imagefname,@Rating,@Trailers,@TheatreDateReleased,@maincast)", con1);
    '    SqlCommand cmd = new SqlCommand("SELECT MovieTitle,MovieDirector from Movie WHERE TheatreId=@TheatreId AND MovieTitle=@MovieTitle AND MovieDirector=@MovieDirector ", con);
    '    string ParamString = lblId.Text;
    '    Boolean Showing;
    '    Int32 RowCount;
    '    Boolean HasMovies;

    '    cmd.Parameters.AddWithValue("@TheatreId", ParamString);
    '    cmd.Parameters.AddWithValue("@MovieDirector", txtMovieDirectorI.Text);
    '    cmd.Parameters.AddWithValue("@MovieTitle", txtMovieTitleI.Text);

    '    using (con)
    '    {
    '        con.Open();
    '        SqlDataReader Reader = cmd.ExecuteReader();

    '        if (Reader.HasRows)
    '        {

    '            Label15.Text = "The movie " + txtMovieTitleI.Text + " already exist in the database!";

    '            lblCheck.Text = string.Empty;
    '            ClientScript.RegisterClientScriptBlock(ClientScript.GetType(), "Focus", "<script language='JavaScript' 'showInsert();'/>");

    '            HasMovies = true;

    '        }
    '        else
    '        {
    '            HasMovies = false;

    '        }
    '    }
    '    using (con1)
    '    {
    '        con1.Open();
    '        if (HasMovies == false)
    '        {
    '            if (chkShowingI.Checked)
    '            {
    '                Showing = true;

    '            }
    '            else
    '            {
    '                Showing = false;
    '            }
    '            cmd1.Parameters.AddWithValue("@CategoryId", ddlCategoryI.SelectedValue);
    '            cmd1.Parameters.AddWithValue("@MovieTitle", txtMovieTitleI.Text);
    '            cmd1.Parameters.AddWithValue("@MovieDirector", txtMovieDirectorI.Text);
    '            cmd1.Parameters.AddWithValue("@Showing", Showing);
    '            cmd1.Parameters.AddWithValue("@MovieDescription", txtMovieDescriptionI.Text);
    '            cmd1.Parameters.AddWithValue("@TheatreId", lblId.Text);
    '            cmd1.Parameters.AddWithValue("@MovieDuration", txtMovieDurationI.Text);
    '            cmd1.Parameters.AddWithValue("@imagefname", txtimagefnameI.Text);
    '            cmd1.Parameters.AddWithValue("@Rating", txtRatingI.Text);
    '            cmd1.Parameters.AddWithValue("@Trailers", txtTrailersI.Text);
    '            cmd1.Parameters.AddWithValue("@TheatreDateReleased", txtTheatreDateReleasedI.Text);
    '            cmd1.Parameters.AddWithValue("@maincast", txtMaincastI.Text);
    '            RowCount = cmd1.ExecuteNonQuery();
    '            ddlCategoryI.SelectedIndex = -1;
    '            txtMovieTitleI.Text = string.Empty;
    '            txtMovieDirectorI.Text = string.Empty;
    '            chkShowingI.Checked = false;
    '            txtMovieDescriptionI.Text = string.Empty;
    '            txtMovieDurationI.Text = string.Empty;
    '            txtimagefnameI.Text = string.Empty;
    '            txtRatingI.Text = string.Empty;
    '            txtTrailersI.Text = string.Empty;
    '            txtTheatreDateReleasedI.Text = string.Empty;
    '            txtMaincastI.Text = string.Empty;

    '            if (RowCount > 0)
    '            {
    '                Label16.Text = "The Movie " + txtMovieTitleI.Text + " has been added sucessfully!";
    '                lblCorrect.Visible = true;


    '            }
    '        }

    '    }
    '    grdMovie.DataBind();
    '}
    Private Sub Page_Load()
        If Not Page.IsPostBack Then
            grdMovie.SelectedIndex = 0
        End If
        Label15.Text = String.Empty
        Label16.Text = String.Empty

    End Sub
    Protected Sub Clear(ByVal sender As Object, ByVal e As EventArgs)
        Label15.Text = String.Empty
        Label16.Text = String.Empty
    End Sub
End Class
